package com.lfd.rabbitmq.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

/**
 * @项目名： spring-emq
 * @包名： com.lfd.rabbitmq.config
 * @文件名: NewRabbitConfig
 * @创建者: liufudong@szyh-smart.com
 * @创建时间: 2024/10/11 19:50
 * @描述：
 */
@Slf4j
@Configuration
public class NewRabbitConfig {

    public static final String EXCHANGE_NAME_A = "reliable_exchange";
    public static final String QUEUE_NAME_A = "reliable_queue";
    public static final String QUEUE_NAME_B = "reliable_queue_B";
    public static final String ROUTING_KEY_A = "reliable_routing_key";

//    @Bean
//    public CachingConnectionFactory connectionFactory() {
//        CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
//        connectionFactory.setPublisherConfirmType(CachingConnectionFactory.ConfirmType.CORRELATED);
//        connectionFactory.setPublisherReturns(true);
//        return connectionFactory;
//    }
//
//    @Bean
//    public RabbitTemplate rabbitTemplate(CachingConnectionFactory connectionFactory) {
//        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
//        // 设置 mandatory，确保消息路由失败时返回
//        rabbitTemplate.setMandatory(true);
//        // 配置 ConfirmCallback 确认消息是否到达交换机
//        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
//            @Override
//            public void confirm(CorrelationData correlationData, boolean ack, String s) {
//
//                try {
//                    if (correlationData != null && correlationData.getReturned() != null) {
//                        String string = new String(correlationData.getReturned().getMessage().getBody(), "UTF-8");
//                        log.info("NewRabbitConfig confirm s:{}", string);
//                    }
//                    if (ack) {
//                        log.info("NewRabbitConfig Message delivered to exchange successfully ");
//                    } else {
//                        log.info("NewRabbitConfig Message delivered to exchange failed");
//                        // 可以重试或者记录日志
//                    }
//                } catch (UnsupportedEncodingException e) {
//                    throw new RuntimeException(e);
//                }
//
//            }
//        });
//        // 配置 ReturnCallback 确认消息是否路由到队列
//        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
//            @Override
//            public void returnedMessage(ReturnedMessage message) {
//                //处理无法路由的消息
//                log.info("NewRabbitConfig Message returned: {}", new String(message.getMessage().getBody()));
//                String returnedMsg = new String(message.getMessage().getBody(), StandardCharsets.UTF_8);
//                log.info("Message returned: {}", returnedMsg);
//                log.error("Failed to route message to queue. ReplyCode: {}, ReplyText: {}, Exchange: {}, RoutingKey: {}",
//                        message.getReplyCode(),
//                        message.getReplyText(),
//                        message.getExchange(),
//                        message.getRoutingKey());
//            }
//        });
//        return rabbitTemplate;
//    }
//
//    // 声明交换机
//    @Bean
//    public DirectExchange newExchange() {
//        return new DirectExchange(EXCHANGE_NAME_A);
//    }
//
//    // 声明持久化队列
//    @Bean
//    public Queue newQueue() {
//        return QueueBuilder.durable(QUEUE_NAME_A).build();
//    }
//
//    // 将队列绑定到交换机
//    @Bean
//    public Binding newBinding(Queue newQueue, DirectExchange newExchange) {
//        return BindingBuilder.bind(newQueue).to(newExchange).with(ROUTING_KEY_A);
//    }

}
